home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / util / wrap_1.00 / Wrap.c next >
C/C++ Source or Header  |  1990-03-04  |  9KB  |  306 lines

  1. #include <exec/types.h>
  2. #include <exec/exec.h>
  3. #include <libraries/dos.h>
  4. #include <libraries/dosextens.h>
  5. #include <ctype.h>
  6. #define CR 0x0D
  7. #define LF 0x0A
  8.  
  9. UBYTE GetWord();
  10. void SetVariables();
  11.  
  12. BYTE DoubleSpacing = FALSE;
  13. USHORT RightMargin = 0;
  14. USHORT LeftMargin = 0;
  15. USHORT TopMargin = 0;
  16. USHORT BottomMargin = 0;
  17. SHORT PageSize = 66;
  18. USHORT TabSpaces = 5;
  19. USHORT ParaIndent = 0;
  20.  
  21. main(argc,argv)
  22. int argc;
  23. char *argv[];
  24. {
  25.    struct FileHandle *infile,*outfile;  /*pointers to files*/
  26.    char chip outbuf[256];
  27.    UBYTE EOP=FALSE;     /*End of paragraph flag*/
  28.    UBYTE EOF=FALSE;   /*End of file flag*/
  29.    UBYTE wordcount,linecount;
  30.    ULONG Line;
  31.    char chip LeftSpaces[80];
  32.    char chip TopReturns[80];
  33.    char chip IndentSpaces[80];
  34.    char chip BottomReturns[80];
  35.    
  36.    LeftSpaces[0]=0x0a;
  37.    LeftSpaces[1]=NULL;
  38.    TopReturns[0]=BottomReturns[0]=NULL;
  39.    
  40.    if(argv[1][0]=='?')  /*Print the instructions*/
  41.    {
  42.       puts("Wrap V1.0 (c)1989, 1990 by Dave Schreiber.  All rights reserved.");
  43.       puts("Written by Dave Schreiber.");
  44.       puts("Format:");
  45.       puts("  1> wrap [options] Infile Outfile");
  46.       puts("  Infile-text file to be processed");
  47.       puts("  Outfile-filename that the formatted text is to be written to");
  48.       puts("  Options (in upper- or lowercase");
  49.       puts("    -a# -- number of spaces to expand tabs out to (default 5)");
  50.       puts("    -b# -- bottom margin (default 0)");
  51.       puts("    -t# -- top margin (default 0)");
  52.       puts("    -r# -- right margin (default 0)");
  53.       puts("    -l# -- left margin (default 0)");
  54.       puts("    -i# -- number of spaces to indent each paragraph (default 0)");
  55.       puts("    -p# -- page size (with no margin, default 66)");
  56.       puts("    -d  -- double spaced (default is single spaced)");
  57.       exit(0);
  58.    }
  59.    
  60.    if((argv[argc-1][0]=='-') || (argc == 2)) /*Check for output filename*/
  61.    {
  62.       puts("No output file specified!!!\n");
  63.       exit(100);
  64.    }
  65.    
  66.    if((argv[argc-2][0]=='-') || (argc == 1)) /*Check for input filename*/
  67.    {
  68.       puts("No input file specified!!!\n");
  69.       exit(200);
  70.    }
  71.    
  72.       /*Get the command line switches, etc.*/
  73.    SetVariables(argc,argv,LeftSpaces,TopReturns,BottomReturns,IndentSpaces);
  74.    if(79-RightMargin-LeftMargin < 1)
  75.    {
  76.       puts("The left/right margins are too large!\n");
  77.       exit(300);
  78.    }
  79.    
  80.    /*Get the true page size (after the margins are taken out)*/
  81.    PageSize = PageSize - (TopMargin + BottomMargin);
  82.    if(PageSize < 1)
  83.    {
  84.       puts("The top/bottom margins are too large!");
  85.       exit(400);
  86.    }
  87.          
  88.    /*Open read file*/
  89.    if((infile=(struct FileHandle *)Open(argv[argc-2],MODE_OLDFILE))==NULL)
  90.    {
  91.       puts("Couldn't open the read file!");
  92.       exit(500);
  93.    }
  94.  
  95.    /*Now go for write file*/
  96.    if((outfile=(struct FileHandle *)Open(argv[argc-1],MODE_NEWFILE))==NULL)
  97.    {
  98.       puts("Couldn't open the write file!");
  99.       Close(infile); /*Clean up*/
  100.       exit(600);
  101.    }
  102.  
  103.    /*Write the first top margin padding*/
  104.    Write(outfile,&TopReturns,TopMargin);
  105.    
  106.    /*Get the first word*/
  107.    linecount=wordcount=GetWord(outbuf,&EOP,&EOF,infile);
  108.  
  109.    /*Write out the left margin*/
  110.    Write(outfile,&LeftSpaces[1],LeftMargin);
  111.    
  112.    /*Write out the paragraph indent*/
  113.    Write(outfile,IndentSpaces,ParaIndent);
  114.    linecount+=ParaIndent;
  115.    
  116.    /*Write the first word*/
  117.    Write(outfile,outbuf,wordcount);
  118.    wordcount=(linecount+=LeftMargin);
  119.    
  120.    /*First line of the page...*/
  121.    Line = 0;
  122.    
  123.    while(!EOF) /*While there's still stuff to be processed...*/
  124.    {
  125.       outbuf[1]=NULL;
  126.       wordcount=GetWord(&outbuf[1],&EOP,&EOF,infile)+1; /*Get a word*/
  127.  
  128.       if((linecount+=wordcount) > 79-RightMargin) /*End of line*/
  129.       {
  130.          
  131.          if(DoubleSpacing)
  132.          {
  133.             Line++;
  134.             outbuf[0]=0x0a;      /*If double spaced, write out an*/
  135.             Write(outfile,outbuf,1);   /*extra line*/
  136.          }
  137.  
  138.          if(++Line == PageSize) /*If we're at the end of a page...*/
  139.          {
  140.             Write(outfile,BottomReturns,BottomMargin); /*Write out the margins*/
  141.             Write(outfile,TopReturns,TopMargin);
  142.             Line = 0;   /*And start a new page*/
  143.          }
  144.  
  145.          Write(outfile,LeftSpaces,LeftMargin+1); /*Left margin*/
  146.          Write(outfile,&outbuf[1],wordcount-1); /*Write the word*/
  147.          linecount=wordcount+LeftMargin;
  148.       }
  149.       else  /*If we're not at the end of a line*/
  150.       {
  151.          if(linecount != (wordcount+LeftMargin)) /*If were not at the start*/
  152.          {                                       /*of a line...*/
  153.             outbuf[0]=0x20;   /*Tack on a space*/
  154.             Write(outfile,outbuf,wordcount);
  155.          }
  156.          else     /*Don't tack on a space (insures new paragraphs are*/
  157.             Write(outfile,&outbuf[1],wordcount-1); /*formatted properly)*/
  158.       }
  159.       
  160.       if(EOP)  /*If we're at the end of a paragraph*/
  161.       {
  162.          outbuf[0]=0x0a;   /*Start a new line*/
  163.          Write(outfile,outbuf,1);
  164.          if(!EOF) /*Do this only if we're not at the end of the text*/
  165.          {
  166.             if(DoubleSpacing) /*Another line if double spaced*/
  167.             {
  168.                Line++;
  169.                Write(outfile,outbuf,1);
  170.             }
  171.             
  172.             if(++Line == PageSize)  /*If at the end of a page?*/
  173.             {     /*Write the margins, etc.*/
  174.                Write(outfile,BottomReturns,BottomMargin);
  175.                Write(outfile,TopReturns,TopMargin);
  176.                Line = 0;
  177.             }
  178.             if(LeftMargin>0) /*Write out a margin if it exists*/
  179.                Write(outfile,&LeftSpaces[1],LeftMargin-1);
  180.             linecount=LeftMargin;
  181.             if(ParaIndent>0)  /*Write out the indent for the start*/
  182.                Write(outfile,IndentSpaces,ParaIndent-1); /*of the*/
  183.                               /*next paragraph*/
  184.             linecount+=ParaIndent;
  185.          }
  186.       }
  187.       
  188.    }
  189.    
  190.    Close(outfile);   /*Done*/
  191.    Close(infile);
  192.    exit(0);
  193. }
  194.  
  195. UBYTE GetWord(buffer,EOP,EOF,in)
  196. char *buffer;
  197. UBYTE *EOP,*EOF;
  198. struct FileHandle *in;
  199. {
  200.    char chip inbuf[8];
  201.    UBYTE c,wordcount=0;
  202.    
  203.    *EOP=FALSE;
  204.    *EOF=GetByte(inbuf,in);
  205.    while ( ( !(*EOF) ) && (inbuf[0] != 0x0a) && (inbuf[0] != 0x20) )
  206.    {
  207.       if(inbuf[0]=='\t')  /*Expand tab out to spaces*/
  208.          for(c=0;c<TabSpaces;buffer[c++]=' ',wordcount++);
  209.       else 
  210.          buffer[wordcount++]=inbuf[0]; /*Just stick it in otherwise*/
  211.       *EOF=GetByte(inbuf,in);
  212.    }
  213.          
  214.    if(inbuf[0]==0x0a || *EOF) /*If we're at the end of a paragraph...*/
  215.       *EOP=TRUE;
  216.       
  217.    return(wordcount);
  218. }
  219.  
  220. /*This routine reads a small chuck (128) of a file at once and then*/
  221. /*lets the calling function get it a byte at a time.  Why not just  */
  222. /*read off the disk a byte at a time?  Speed.  For example, on my   */
  223. /*hard drive equipped system, this speeds up processing 400%        */
  224.  
  225. GetByte(buffer,infile)
  226. char *buffer;
  227. struct FileHandle *infile;
  228. {
  229.    static char chip Buffer[128];
  230.    static curpos=0; /*Next byte to be returned*/
  231.    static end = 0;  /*Last byte in buffer*/
  232.    
  233.    if(curpos == end) /*If we've sent every byte*/
  234.    {                 /*Get another 8K chunk*/
  235.       curpos = 0;
  236.       end=Read(infile,Buffer,sizeof(Buffer));
  237.    }
  238.    
  239.    buffer[0]=Buffer[curpos++];
  240.    return(!end); /*Returns whether or not EOF*/
  241. }
  242.  
  243. void SetVariables(argc,argv,LeftSpaces,TopReturns,BottomReturns,
  244.                   IndentSpaces)
  245. int argc;
  246. char *argv[];
  247. char *LeftSpaces,*TopReturns,*BottomReturns,*IndentSpaces;
  248. {
  249.    register BYTE c,s;
  250.    int value;
  251.    
  252.    for(c=1;c<argc-2;c++)
  253.    {
  254.       switch(argv[c][1])   /*Get each switch*/
  255.       {
  256.          case 'd':   /*Double spacing*/
  257.          case 'D':   /*(Lowercase and uppercase, just to be extra friendly)*/
  258.             DoubleSpacing=TRUE;
  259.             break;
  260.          case 'r':
  261.          case 'R':   /*Right margin*/
  262.             stcd_i(&argv[c][2],&value);
  263.             RightMargin = value;
  264.             break;
  265.          case 'l':
  266.          case 'L':   /*Left margin*/
  267.             stcd_i(&argv[c][2],&value);
  268.             LeftMargin = value;
  269.             for(s=0;s < LeftMargin;LeftSpaces[++s]=' ');
  270.             LeftSpaces[s+1]=NULL;
  271.             break;
  272.          case 't':   /*Set top margin*/
  273.          case 'T':   
  274.             stcd_i(&argv[c][2],&value);
  275.             TopMargin = value;
  276.             for(s=0;s < TopMargin;TopReturns[s++]=0x0a);
  277.             TopReturns[s]=NULL;
  278.             break;
  279.          case 'b':   /*Set the bottom margin*/
  280.          case 'B':
  281.             stcd_i(&argv[c][2],&value);
  282.             BottomMargin = value;
  283.             for(s=0;s < BottomMargin;BottomReturns[s++]=0x0a);
  284.             BottomReturns[s]=NULL;
  285.             break;
  286.          case 'p':   /*Set the page size*/
  287.          case 'P':
  288.             stcd_i(&argv[c][2],&value);
  289.             PageSize = value;
  290.             break;
  291.          case 'a':   /*Set the number of spaces a TAB represents*/
  292.          case 'A':
  293.             stcd_i(&argv[c][2],&value);
  294.             TabSpaces = value;
  295.             break;
  296.          case 'i':
  297.          case 'I':
  298.             stcd_i(&argv[c][2],&value);
  299.             ParaIndent = value;
  300.             for(s=0;s < ParaIndent && s < 80;IndentSpaces[s++]=' ');
  301.             IndentSpaces[s]=NULL;
  302.             break;
  303.       }
  304.    }
  305. }
  306.